/**
 *
 */
package com.cmcc.awareness;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.pcap4j.packet.Packet;

/**
 * @Date 2018年4月11日 上午10:40:41
 * @author Amish
 * 
 * 报文长度(负载部分)表达式：l
 * 
 * eg:
 * {l|0|32}
   {l|1|48}
   {l|2|2}
 */
public class ProtoDatProtocolFeatureCodeElement_l implements ProtoDatProtocolFeatureCodeElement{
	private static Logger logger = LogManager.getLogger(ProtoDatProtocolFeatureCodeElement_l.class);
	
	String initialStr;
	String operator;
	int dataLen;
	
	public String appName;
	public String protocolType;
	public ProtoDatProtocolFeatureCodeElement_l(String appName,String protocolType){
		this.appName = appName;
		this.protocolType = protocolType;
	}
	
	@Override
	public String getType() {	
		return "l";
	}

	@Override
	public boolean distinguish(String protocolType,jpcap.packet.Packet packet) {
		
		try{
			if(packet.data == null)
				return false;

			int packetDataLen = packet.data.length;
//			if("TCP".equals(protocolType)){
//				TCPPacket tp = (TCPPacket) packet;
//				packetDataLen = tp.data.length;
//				
//			}else if("UDP".equals(protocolType)){
//				UDPPacket up = (UDPPacket) packet;
//				packetDataLen = up.data.length;
//			}else{
//				return false;
//			}
			
			switch(operator) {
			case "0":// =
				if(packetDataLen == dataLen){
					//System.out.println(this.toString());
					return true;
				}
				return false;
				//break;
			case "1":// >
				if(packetDataLen > dataLen){
					//System.out.println(this.toString());
					return true;
				}
				return false;
				//break;
			case "2":// <
				if(packetDataLen < dataLen){
					//System.out.println(this.toString());
					return true;
				}
				return false;
				//break;
			default:
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Distinguish Exception -- <"+this.appName+">"+"<"+this.protocolType+"> : "+this.toString());
			return false;
		}
		

	}

	@Override
	public void parseSelf(String element) {
		
		try{			
			this.initialStr = element;
		    String[] subs = element.split("\\|");
		    this.operator = subs[1];
		    this.dataLen = Integer.valueOf(subs[2]).intValue();

		}catch(Exception e){
			e.printStackTrace();
		}

	    
	}
	
	public String toString(){
		return "{"+initialStr+"}";
	}

	@Override
	public boolean distinguish(String protocolType, Packet packet) {
		try{
			if(packet.getRawData() == null)
				return false;

			int packetDataLen = packet.getRawData().length;
//			if("TCP".equals(protocolType)){
//				TCPPacket tp = (TCPPacket) packet;
//				packetDataLen = tp.data.length;
//				
//			}else if("UDP".equals(protocolType)){
//				UDPPacket up = (UDPPacket) packet;
//				packetDataLen = up.data.length;
//			}else{
//				return false;
//			}
			
			switch(operator) {
			case "0":// =
				if(packetDataLen == dataLen){
					//System.out.println(this.toString());
					return true;
				}
				return false;
				//break;
			case "1":// >
				if(packetDataLen > dataLen){
					//System.out.println(this.toString());
					return true;
				}
				return false;
				//break;
			case "2":// <
				if(packetDataLen < dataLen){
					//System.out.println(this.toString());
					return true;
				}
				return false;
				//break;
			default:
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Distinguish Exception -- <"+this.appName+">"+"<"+this.protocolType+"> : "+this.toString());
			return false;
		}
	}

}
 